import subprocess, os
BASE_DIR = os.path.dirname(os.path.abspath(__file__))

def build_ui(name):
    src = os.path.join(BASE_DIR, 'resource', f'{name}.ui')
    dst = os.path.join(BASE_DIR, 'generate', f'Ui_{name}.py')
    os.system(f'pyuic5 {src} -o {dst}')


def build_qrc(name):
    src = os.path.join(BASE_DIR, 'resource', f'{name}.qrc')
    dst = os.path.join(BASE_DIR, 'generate', f'Qrc_{name}.py')
    os.system(f'pyrcc5 {src} -o {dst}')


main_file = 'main.py'
ui_files = ['MainWindow']
qrc_files = ['resource']


import argparse
parser = argparse.ArgumentParser()
parser.add_argument("-b", "--build",
                    help="Build ui and qrc.",
                    action="store_true")
parser.add_argument("-r", "--release",
                    help="Release with pyinstaller.",
                    action="store_true")
args = parser.parse_args()

if args.build:
    for name in ui_files:
        build_ui(name)
    for name in qrc_files:
        build_qrc(name)

if args.release:
    os.system(f'pyinstaller -w {main_file}')

# images = os.listdir('./icons')
# qss = os.listdir('./qss')
# f = open('images.qrc', 'w+')
# f.write(u'<!DOCTYPE RCC>\n<RCC version="1.0">\n<qresource>\n')
#
# for item in images:
#     item1 = item.split('.')[0]
#     f.write(u'<file alias="' + item1 + '">icons/' + item + '</file>\n')
#
# for item in qss:
#     f.write(u'<file alias="qss/'+ item +'">qss/'+ item +'</file>\n')
#
# f.write(u'</qresource>\n</RCC>')
# f.close()

# src = os.path.join(BASE_DIR, f'{"images"}.qrc')
# dst = os.path.join(BASE_DIR, f'{"images"}.py')
# pipe = subprocess.Popen(f'pyrcc5 -o {dst} {src}', stdout = subprocess.PIPE, stdin = subprocess.PIPE, stderr = subprocess.PIPE, creationflags=0x08)

pipe = subprocess.Popen(r'pyrcc5 -o images.py images.qrc', stdout = subprocess.PIPE, stdin = subprocess.PIPE, stderr = subprocess.PIPE, creationflags=0x08)

